package com.loukou.lkt.api.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loukou.lkt.lkt.dao.CheckingOrderDao;
import com.loukou.lkt.lkt.dao.CheckingOrderDetailDao;
import com.loukou.lkt.lkt.dao.GoodsDao;
import com.loukou.lkt.lkt.dao.WarehouseDao;
import com.loukou.lkt.lkt.entity.CheckingOrder;
import com.loukou.lkt.lkt.entity.CheckingOrderDetail;
import com.loukou.lkt.lkt.entity.Goods;
import com.loukou.lkt.lkt.entity.Warehouse;
import com.loukou.lkt.lkt.enums.CheckingOrderCheckingStateEnum;
import com.loukou.lkt.lkt.enums.WmsInOrderTypeEnum;
import com.loukou.lkt.lkt.enums.WmsOutOrderTypeEnum;
import com.loukou.lkt.lkt.req.AllocationStockRealDetailReq;
import com.loukou.lkt.lkt.req.AllocationStockRealReq;
import com.loukou.lkt.lkt.req.WmsCheckCallbackReq;
import com.loukou.lkt.lkt.req.WmsCheckOrderCallbackReq;
import com.loukou.lkt.lkt.req.WmsCheckOutOrderCallbackDetailReq;
import com.loukou.lkt.lkt.req.WmsInOrderCallbackDetailReq;
import com.loukou.lkt.lkt.req.WmsInOrderCallbackReq;
import com.loukou.lkt.lkt.req.WmsOutOrderCallbackDetailReq;
import com.loukou.lkt.lkt.req.WmsOutOrderCallbackReq;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.service.AllocationOrderService;
import com.loukou.lkt.lkt.service.GoodsCostService;
import com.loukou.lkt.lkt.service.GoodsStockService;
import com.loukou.lkt.lkt.service.PoPurchaseOrderService;
import com.loukou.lkt.lkt.service.PoPurchaseReturnOrderService;
import com.loukou.lkt.lkt.service.SaleOrderService;
import com.loukou.lkt.lkt.service.StockInOrderService;
import com.loukou.lkt.lkt.service.StockOutOrderService;

/**
 * WMS回调接口
 * @author wr
 *
 */
@Controller
@RequestMapping("/api/wmslktcallback")
public class WmsLktCallbackController {
	@Autowired
	private PoPurchaseOrderService poPurchaseOrderService;
	@Autowired
	private PoPurchaseReturnOrderService poPurchaseReturnOrderService;
	@Autowired
	private AllocationOrderService allocationOrderService;
	@Autowired
	private StockInOrderService stockInOrderService;
	@Autowired
	private StockOutOrderService stockOutOrderService;
	@Autowired
	private SaleOrderService saleOrderService;
	@Autowired
	private GoodsStockService goodsStockService;
	@Autowired
	private GoodsCostService goodsCostService;
	@Autowired
	private CheckingOrderDao checkingOrderDao;
	@Autowired
	private CheckingOrderDetailDao checkingOrderDetailDao;
	@Autowired
	private WarehouseDao warehouseDao;
	@Autowired
	private GoodsDao goodsDao;
	
	//WMS入库回调
	@RequestMapping(value="/wmsinorder", method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	@Transactional
    public RespDto<String> wmsInOrder(@RequestBody WmsInOrderCallbackReq req){
		RespDto<String> resp = new RespDto<String>();
		if (req==null || (req.getOrderType()!=WmsInOrderTypeEnum.TYPE_PURCHASE.getType() && req.getOrderType()!=WmsInOrderTypeEnum.TYPE_ALLOCATION.getType())) {
			resp.setCode(400);
			resp.setMsg("参数有误");
			return resp;
		}
		if(req.getDetails() == null || req.getDetails().size() == 0){
			resp.setCode(400);
			resp.setMsg("参数有误");
			return resp;
		}
		
		//新增入库单
		RespDto<Integer> stockInOrder=stockInOrderService.addStockInOrder(req);
		int stockInOrderId=stockInOrder.getResult();
		
		//更新采购单
		if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_PURCHASE.getType()){
			resp = poPurchaseOrderService.updatePurchase(stockInOrderId,req);
		}
		
		//调拨
		if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_ALLOCATION.getType()){
			AllocationStockRealReq allocationStockReal = new AllocationStockRealReq();
			allocationStockReal.setInOrOut("in");
			allocationStockReal.setOrderCode(req.getOutbizCode());
			allocationStockReal.setWmsOrderCode(req.getInOrderCode());
			List<AllocationStockRealDetailReq> allocationDetailList = new ArrayList<AllocationStockRealDetailReq>();
			if(req.getDetails() != null){
				for(WmsInOrderCallbackDetailReq detailReq: req.getDetails()){
					AllocationStockRealDetailReq allocationStockRealDetailReq = new AllocationStockRealDetailReq();
					allocationStockRealDetailReq.setAllocationDetailId(Integer.parseInt(detailReq.getOutbizId()));
					allocationStockReal.setTime(req.getReceiveTime());
					allocationStockRealDetailReq.setRealQuantity(detailReq.getReceiveNumber());
					allocationDetailList.add(allocationStockRealDetailReq);
				}
			}
			allocationStockReal.setDetails(allocationDetailList);
			resp = allocationOrderService.wmsRealStockInOut(allocationStockReal);
		}
		
		//更新本地库存、更新本地库存log
		RespDto<Integer> updateResult=goodsStockService.updateStockInOrder(req);
		if(updateResult.getCode()!=200){
			resp.setCode(updateResult.getCode());
			resp.setMsg(updateResult.getMsg());
			return resp;
		}
		
		//只有采购才更新成本价
		if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_PURCHASE.getType()){
			//更新成本价
			RespDto<Integer> goodsCostResult=goodsCostService.addGoodsCost(req);
			if(goodsCostResult.getCode()!=200){
				resp.setCode(goodsCostResult.getCode());
				resp.setMsg(goodsCostResult.getMsg());
				return resp;
			}
		}
		
        return resp;
    }
	
	//WMS出库回调
	@RequestMapping(value="/wmsoutorder", method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	@Transactional
    public RespDto<String> wmsOutOrder(@RequestBody WmsOutOrderCallbackReq req){
		RespDto<String> resp = new RespDto<String>();		
		if (req==null || (req.getOrderType()!=WmsOutOrderTypeEnum.TYPE_RETURN.getType() && req.getOrderType()!=WmsOutOrderTypeEnum.TYPE_ALLOCATION.getType())) {
			resp.setCode(400);
			resp.setMsg("参数有误");
			return resp;
		}
		if(req.getDetails() == null || req.getDetails().size() == 0){
			resp.setCode(400);
			resp.setMsg("参数有误");
			return resp;
		}
		if(req.getDetails() == null || req.getDetails().size() == 0){
			resp.setCode(400);
			resp.setMsg("参数有误");
			return resp;
		}
		//新增出库单
		RespDto<Integer> stockOutOrder=stockOutOrderService.addStockOutOrder(req);
		if(stockOutOrder.getCode()!=200){
			resp.setCode(stockOutOrder.getCode());
			resp.setMsg(stockOutOrder.getMsg());
			return resp;
		}
		int stockOutOrderId=stockOutOrder.getResult();
		 
		//退货
		if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_RETURN.getType()){
			RespDto<String> result = poPurchaseReturnOrderService.updatePurchaseReturn(stockOutOrderId,req);
			resp.setCode(result.getCode());
			resp.setMsg(result.getMsg());
		}
		
		//调拨
		if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_ALLOCATION.getType()){
			AllocationStockRealReq allocationStockReal = new AllocationStockRealReq();
			allocationStockReal.setInOrOut("out");
			allocationStockReal.setOrderCode(req.getOutbizCode());
			allocationStockReal.setTime(req.getDeliveryTime());
			allocationStockReal.setWmsOrderCode(req.getOutOrderCode());
			List<AllocationStockRealDetailReq> allocationDetailList = new ArrayList<AllocationStockRealDetailReq>();
			if(req.getDetails() != null){
				for(WmsOutOrderCallbackDetailReq detailReq: req.getDetails()){
					AllocationStockRealDetailReq allocationStockRealDetailReq = new AllocationStockRealDetailReq();
					allocationStockRealDetailReq.setAllocationDetailId(Integer.parseInt(detailReq.getOutbizId()));
					allocationStockRealDetailReq.setRealQuantity(detailReq.getDeliveryNumber());
					allocationDetailList.add(allocationStockRealDetailReq);
				}
			}
			allocationStockReal.setDetails(allocationDetailList);
			allocationOrderService.wmsRealStockInOut(allocationStockReal);
		}
		
		//更新本地库存、更新本地库存log
		RespDto<Integer> updateResult=goodsStockService.updateStockOutOrder(req);
		if(updateResult.getCode()!=200){
			resp.setCode(updateResult.getCode());
			resp.setMsg(updateResult.getMsg());
			return resp;
		}
        return resp;
    }
	
	//WMS盘点推送
	@RequestMapping(value="/wmscheckorder", method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
    public RespDto<String> wmsCheckOrder(@RequestBody WmsCheckCallbackReq req){
		RespDto<String> resp = new RespDto<String>();
		if (req==null || req.getWmsCheckOrderCallback()==null) {
			resp.setCode(400);
			resp.setMsg("参数有误");
			return resp;
		}
		//获取盘点单回调
		WmsCheckOrderCallbackReq wmsCheckOrderCallbackReq=req.getWmsCheckOrderCallback();
		
		//获取出库单号
		String outorderCode="";
		if(req.getWmsOutOrderCallback()!=null){
			outorderCode=req.getWmsOutOrderCallback().getOutOrderCode();
		}
		//获取入库单号
		String inorderCode="";
		if(req.getWmsInOrderCallback()!=null){
			inorderCode=req.getWmsInOrderCallback().getInOrderCode();
		}
		
		//获取仓库ID
		Warehouse warehouseMsg=warehouseDao.findByWarehouseCode(wmsCheckOrderCallbackReq.getWarehouseNo());
		
		//新增盘点单
		CheckingOrder checkingOrder=new CheckingOrder();
		checkingOrder.setOrderCode(wmsCheckOrderCallbackReq.getCheckOrderCode());
		checkingOrder.setInorderCode(inorderCode);
		checkingOrder.setOutorderCode(outorderCode);
		checkingOrder.setWarehouseId(warehouseMsg.getWarehouseId());
		checkingOrder.setCheckingState(CheckingOrderCheckingStateEnum.TYPE_CHECKED.getType());
		checkingOrder.setCreater(wmsCheckOrderCallbackReq.getCreater());
		checkingOrder.setCreateTime(wmsCheckOrderCallbackReq.getCreateTime());
		checkingOrder.setRemark(wmsCheckOrderCallbackReq.getRemark());
		checkingOrder.setWarehouseName(warehouseMsg.getWarehouseName());
		checkingOrder.setWarehouseNo(wmsCheckOrderCallbackReq.getWarehouseNo());
		checkingOrderDao.save(checkingOrder);
		
		//新增盘点明细
		List<CheckingOrderDetail> cDetailList=new ArrayList<CheckingOrderDetail>();
		for(WmsCheckOutOrderCallbackDetailReq cDetail:wmsCheckOrderCallbackReq.getDetails()){
			//获取商品信息
			Goods goodsMsg=goodsDao.findOne(cDetail.getGoodsId());
			
			CheckingOrderDetail checkingOrderDetail=new CheckingOrderDetail();
			checkingOrderDetail.setCheckingId(checkingOrder.getId());
			checkingOrderDetail.setGoodsId(goodsMsg.getGoodsId());
			checkingOrderDetail.setPositionType(cDetail.getPositionType());
			checkingOrderDetail.setBookNumber(cDetail.getBookNumber());
			checkingOrderDetail.setProfitandlossNumber(cDetail.getProfitandlossNumber());
			checkingOrderDetail.setCheckingNumber(cDetail.getCheckingNumber());
			checkingOrderDetail.setSku(cDetail.getSku());
			checkingOrderDetail.setBarCode(cDetail.getBarCode());
			checkingOrderDetail.setGoodsName(goodsMsg.getGoodsName());
			checkingOrderDetail.setExpiryDate(cDetail.getExpiryDate());
			checkingOrderDetail.setCreateTime(cDetail.getCreateTime());
			cDetailList.add(checkingOrderDetail);
		}
		checkingOrderDetailDao.save(cDetailList);
		
		if(req.getWmsOutOrderCallback()!=null){
			//获取出库单回调
			WmsOutOrderCallbackReq wmsOutOrderCallbackReq=req.getWmsOutOrderCallback();

			//新增出库单
			RespDto<Integer> stockOutOrder=stockOutOrderService.addStockOutOrder(wmsOutOrderCallbackReq);
			if(stockOutOrder.getCode()!=200){
				resp.setCode(stockOutOrder.getCode());
				resp.setMsg(stockOutOrder.getMsg());
				return resp;
			}
			//出库更新本地库存、更新本地库存log
			RespDto<Integer> updateResult=goodsStockService.updateStockOutOrder(wmsOutOrderCallbackReq);
			if(updateResult.getCode()!=200){
				resp.setCode(updateResult.getCode());
				resp.setMsg(updateResult.getMsg());
				return resp;
			}
		}
		
		if(req.getWmsInOrderCallback()!=null){
			//获取入库单回调
			WmsInOrderCallbackReq wmsInOrderCallbackReq=req.getWmsInOrderCallback();
			
			//新增入库单
			RespDto<Integer> stockInOrder=stockInOrderService.addStockInOrder(wmsInOrderCallbackReq);
			if(stockInOrder.getCode()!=200){
				resp.setCode(stockInOrder.getCode());
				resp.setMsg(stockInOrder.getMsg());
				return resp;
			}
			//出库更新本地库存、更新本地库存log
			RespDto<Integer> updateResult=goodsStockService.updateStockInOrder(wmsInOrderCallbackReq);
			if(updateResult.getCode()!=200){
				resp.setCode(updateResult.getCode());
				resp.setMsg(updateResult.getMsg());
				return resp;
			}
		}
		
		return resp;
	}
}
